package com.mlnx.web.device.service.iml;

import com.mlnx.device.config.DeviceConfig;
import com.mlnx.device.service.DeviceNettyService;
import com.mlnx.device.session.manager.MutilDeviceSessionManager;
import com.mlnx.netty.base.DeviceNettyServer;
import com.mlnx.netty.base.service.DeviceServiceManager;
import com.mlnx.netty.ecg.EcgServerInitializer;
import com.mlnx.netty.weice.SugarServerInitializer;
import com.mlnx.netty.xmbp.XmBpServerInitializer;
import com.mlnx.service.base.properties.DeviceNettyProperties;
import com.mlnx.service.base.properties.MutilDeviceProperties;
import com.mlnx.stream.service.DeviceBroadcastService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

import io.netty.channel.nio.NioEventLoopGroup;

/**
 * Created by amanda.shan on 2019/10/10.
 */
@Configuration
@AutoConfigureAfter(DeviceConfig.class)
public class MutilDeviceNettyServer {

    @Autowired
    private DeviceNettyService deviceNettyService;

    @Autowired
    private DeviceBroadcastService deviceBroadcastService;

    @Autowired
    private MutilDeviceSessionManager sessionManager;

    @Autowired
    private MutilDeviceProperties mutilDeviceProperties;

    @PostConstruct
    public void init() throws Exception {

        DeviceNettyProperties deviceNettyProperties = mutilDeviceProperties.getDeviceNetty();
        if (deviceNettyProperties.isEnable()) {

            DeviceServiceManager.getInstance().setDeviceNettyService(deviceNettyService);
            DeviceServiceManager.getInstance().setDeviceBroadcastService(deviceBroadcastService);

            DeviceNettyServer ecgDeviceNettyServer = new DeviceNettyServer(new NioEventLoopGroup(), new NioEventLoopGroup(),
                    "ecg");
            ecgDeviceNettyServer.start(new EcgServerInitializer(), deviceNettyProperties.getEcgPort());


            DeviceNettyServer bpDeviceNettyServer = new DeviceNettyServer(new NioEventLoopGroup(), new NioEventLoopGroup(),
                    "xmbp");
            bpDeviceNettyServer.start(new XmBpServerInitializer(), deviceNettyProperties.getXmbpPort());

            DeviceNettyServer deviceNettyServer = new DeviceNettyServer(new NioEventLoopGroup(), new NioEventLoopGroup(),
                    "sugar");

            deviceNettyServer.start(new SugarServerInitializer(), deviceNettyProperties.getSugarPort());
        }
    }
}
